<?php
namespace framework\core;
class Framework{
    public function __construct()
    {
        $this->initConst();
        $this->initLoad();
        $configl=$this->loadFramworkConfig();
        $config2=$this->loadCommonConfig();
        $GLOBALS['config']=array_merge($configl,$config2);

        $this->initMCA();
        $config3=$this->loadModuleConfig();
        $GLOBALS['config']=array_merge($GLOBALS['config'],$config3);
//        echo '<pre>';
//        print_r($GLOBALS['config']);
//        echo '</pre>';
        $this->initDispatch();

    }
    public function initConst(){
        define("ROOT_PATH",str_replace('\\','/',getcwd()).'/');
        define("APP_PATH",ROOT_PATH.'application/');
        define("FRAMEWORK_PATH",ROOT_PATH.'framework/');
        //定义一个前台图片样式路径
        define("PUBLIC_HOME",'./application/public/home/');
        //        定义后台图片等样式的路径
        define("PUBLIC_ADMIN",'./application/public/admin/');

        //定义一个上传图片地址保存的路径
        define("UPLOAD_PATH",'./application/public/uploads/');
        //        定义一个保存缩略图地址的常量
        define("THUMB_PATH",'./application/public/static/thumb/');

    }
    public function myLoad($classname){
        if($classname=='Smarty'){
            require './framework/vender/smarty/Smarty.class.php';
        }
        $arr=explode("\\",$classname);
        if($arr[0]=='framework'){
            $basic='./';
        }else{
            $basic='./application/';
        }
        $sub_path=str_replace('\\','/',$classname);
        if(substr($classname,-5,2)=='i_'){
            $fix='.interface.php';
        }else{
            $fix='.class.php';
        }
        $classfile=$basic.$sub_path.$fix;
        if(file_exists($classfile)){
            require $classfile;
        }
    }
    public function initLoad(){
        spl_autoload_register(array($this,"myLoad"));
    }
    public function initMCA(){
        $m=isset($_GET['m'])?$_GET['m']:$GLOBALS['config']['default_module'];
        define("MODULE",$m);
        $c=isset($_GET['c'])?$_GET['c']:$GLOBALS['config']['default_controller'];
        define("CONTROLLER",$c);
        $a=isset($_GET['a'])?$_GET['a']:$GLOBALS['config']['default_action'];
        define("ACTION",$a);
    }
    public function initDispatch(){
        $controller_name=CONTROLLER."Controller";
        $action=ACTION.'Action';
        $c_name=MODULE.'\\controller\\'.$controller_name;
        $controller=new $c_name();
        $controller->$action();
    }
    public function loadFramworkConfig(){
        $config_file=FRAMEWORK_PATH.'conf/config.php';
        return require $config_file;
    }
    public function loadCommonConfig(){
        $config_file=APP_PATH.'common/conf/config.php';
        return require $config_file;
    }
    public function loadModuleConfig(){
        $config_file=APP_PATH.MODULE.'/conf/config.php';
        return require $config_file;
    }
}